﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI.Modules
{
    public partial class StatementAmendment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               // PopulateData();
                btnDeleteAll.Visible = false;
            }
        }

        private void PopulateData()
        {
            Lists lstEntity = new Lists();
            List<Statement> dtstatement = lstEntity.GetUnmatchedStatements(((User)Session["CurrentUserVO"]).CustomerID);

            grdStatement.DataSource = dtstatement;
            grdStatement.DataBind();
        }
        protected void EditRow(object sender, GridViewEditEventArgs e)
        {

            grdStatement.EditIndex = e.NewEditIndex;

            this.PopulateData();

        }
        protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)grdStatement.Rows[e.RowIndex];

            grdStatement.DeleteRow(e.RowIndex);

            // this.PopulateData();



        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            PopulateData();
            btnDeleteAll.Visible = true;

        }

        protected void lnkDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            GridViewRow row = (GridViewRow)img.Parent.Parent;

            Lists listIn = new Lists();
            //int result = listIn.DeleteStatement(row.Cells[1].Text);
            int result = listIn.DeleteInvoice(row.Cells[1].Text);
            this.PopulateData();
        }

        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
          //  List<Invoice> dtRemitterList = new List<Invoice>();

          

            foreach (GridViewRow invoiceRow in grdStatement.Rows)
            {
              
                System.Web.UI.HtmlControls.HtmlInputCheckBox chkSelect = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)invoiceRow.Cells[0].FindControl("chkSelect"));
                if (chkSelect.Checked)
                {
                  
                    Literal ltlInvoiceUniqueID = ((Literal)invoiceRow.Cells[0].FindControl("ltlStatementUniqueID"));
                    Lists obj = new Lists();
                    //obj.DeleteStatement(invoiceRow.Cells[1].Text);
                    obj.DeleteInvoice(invoiceRow.Cells[1].Text);
                }

            }
            this.PopulateData();

        }

        protected void grdStatement_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                double value = 0.00;
                if (e.Row.Cells[3].Text != "" && e.Row.Cells[3].Text != "&nbsp;")
                {
                    value = Convert.ToDouble(e.Row.Cells[3].Text);
                    if (value < 0)
                    {
                        e.Row.BackColor = System.Drawing.Color.Cyan;
                        value *= -1;
                    }
                    e.Row.Cells[3].Text = value.ToString("#,#.00", System.Globalization.CultureInfo.InvariantCulture);
                    e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
                }
                else
                {
                    e.Row.Cells[3].Text = "0.00";
                }

                DateTime dt;
                if (e.Row.Cells[2].Text != "" && e.Row.Cells[2].Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(e.Row.Cells[2].Text);
                    e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[2].Text = "";
                }
            }
        }
    }
}